﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MNNUTEM
{
    public partial class Clientes : Form
    {

        crudCliente cCliente;
        private int rutCliente;

        public Clientes()
        {
            InitializeComponent();
            cCliente = new crudCliente();
        }

        private void updateGridView()
        {
            dataGridView1.DataSource = cCliente.Consultation("select *from Cliente");

        }

        private void Clientes_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = cCliente.Consultation("select *from Cliente");
            updateGridView();
            button4.Enabled = false;
            button3.Enabled = false;
        }

        private void getdatos()
        {
            DataSet DS;
            DS = cCliente.Consultation2("select *from Cliente where rutCliente='" + rutCliente + "'");
            textBox1.Text = DS.Tables[0].Rows[0][0].ToString();
            textBox2.Text = DS.Tables[0].Rows[0][1].ToString();
            textBox3.Text = DS.Tables[0].Rows[0][2].ToString();
            textBox4.Text = DS.Tables[0].Rows[0][4].ToString();
            dateTimePicker1.Text = DS.Tables[0].Rows[0][3].ToString();
            textBox5.Text = DS.Tables[0].Rows[0][5].ToString();
            textBox6.Text = DS.Tables[0].Rows[0][6].ToString();

        }

        private void clean()
        {
            button1.Enabled = true;
            button3.Enabled = false;
            button4.Enabled = false;

            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
           

        }

        private void button3_Click(object sender, EventArgs e)
        {
            cCliente.Delete(rutCliente);
            updateGridView();
            clean();
        }

        private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            rutCliente= int.Parse(dataGridView1[0, e.RowIndex].Value.ToString());
            textBox1.Text = rutCliente.ToString();
            button3.Enabled = true;
            button4.Enabled = true;
            button1.Enabled = false;
            getdatos();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            clean();
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            cCliente.Insert(textBox1, textBox2, textBox3,dateTimePicker1.Value.ToShortDateString(), textBox4, textBox5,textBox6);
            updateGridView();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            cCliente.Set(textBox1,textBox2,textBox3,dateTimePicker1.Value.ToShortDateString(),textBox4,textBox5,textBox6,rutCliente);
            updateGridView();
        }
    }
}
